﻿using Abp.Auditing;
using Abp.Authorization.Roles;
using Abp.Authorization.Users;
using Abp.Configuration;
using Abp.EntityHistory;
using Abp.Localization;
using Abp.Notifications;
using Abp.Organizations;
using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using AbpDevelopmentLog.Authorization.Roles;
using AbpDevelopmentLog.Authorization.Users;
using AbpDevelopmentLog.MultiTenancy;

namespace AbpDevelopmentLog.EntityFrameworkCore
{
    public class AbpDevelopmentLogDbContext : AbpZeroDbContext<Tenant, Role, User, AbpDevelopmentLogDbContext>
    {
        /* Define a DbSet for each entity of the application */
        
        public AbpDevelopmentLogDbContext(DbContextOptions<AbpDevelopmentLogDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ApplicationLanguageText>(b => { b.Property(m => m.Key).HasMaxLength(190); });
            modelBuilder.Entity<EntityChange>(b => { b.Property(m => m.EntityTypeFullName).HasMaxLength(190); });
            modelBuilder.Entity<EntityChangeSet>(b => { b.Property(m => m.Reason).HasMaxLength(190); });
            modelBuilder.Entity<NotificationSubscriptionInfo>(b =>
            {
                b.Property(m => m.EntityTypeName).HasMaxLength(190);
            });
            modelBuilder.Entity<RoleClaim>(b => { b.Property(m => m.ClaimType).HasMaxLength(190); });
            modelBuilder.Entity<Setting>(b =>
            {
                b.Property(m => m.Name).HasMaxLength(190);
            });

            modelBuilder.Entity<UserClaim>(b =>
            {
                b.Property(m => m.ClaimType).HasMaxLength(190);
            });

            modelBuilder.Entity<UserLoginAttempt>(b =>
            {
                b.Property(m => m.UserNameOrEmailAddress).HasMaxLength(190);
            });

            modelBuilder.Entity<UserLogin>(b =>
            {
                b.Property(m => m.ProviderKey).HasMaxLength(190);
            });

            modelBuilder.Entity<UserLoginAttempt>(b =>
            {
                b.Property(m => m.UserNameOrEmailAddress).HasMaxLength(190);
            });

            modelBuilder.Entity<OrganizationUnit>(b =>
                b.Property(m => m.DisplayName).HasColumnType("VARCHAR(128) COLLATE gbk_chinese_ci"));

            modelBuilder.Entity<User>(b =>
            {
                b.Property(m => m.UserName).HasMaxLength(190);
                b.Property(m => m.EmailAddress).HasMaxLength(190);
                b.Property(m => m.NormalizedUserName).HasMaxLength(190);
                b.Property(m => m.NormalizedEmailAddress).HasMaxLength(190);
                b.Property(m => m.Name).HasColumnType("VARCHAR(64) COLLATE gbk_chinese_ci");
                b.Property(m => m.Surname).HasColumnType("VARCHAR(64) COLLATE gbk_chinese_ci");
            });

            modelBuilder.Entity<UserAccount>(b =>
            {
                b.Property(m => m.EmailAddress).HasMaxLength(190);
                b.Property(m => m.UserName).HasMaxLength(190);
            });

            modelBuilder.Entity<AuditLog>(b =>
            {
                b.HasKey(m => new { m.Id, m.ExecutionTime });
                b.Property(m => m.Id).UseMySqlIdentityColumn();
                b.Property(m => m.ExecutionTime).HasDefaultValueSql("CURRENT_TIMESTAMP");

            });

            var prefix = "abcd";
            foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                entity.Relational().TableName =
                    $"{prefix}_{entity.ClrType.Name}s";
            }

        }
    }
}
